﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Business;
using AMM.Business.Project;
using AMM.Model;
using AMM.Model.Business;
using AMM.Model.Project;

namespace AMM.UserControls {
    public partial class PriceEvaluateSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int customerValue = Convert.ToInt32(this.ddlCustomer.SelectedValueEx);
            int productValue = Convert.ToInt32(this.ddlProduct.SelectedValueEx);
            int paymentValue = Convert.ToInt32(this.cmbPayment.SelectedValue);
            int currencyValue = Convert.ToInt32(this.cmbCurrency.SelectedValue);

            PriceEvaluateSearchModel searchResult = new PriceEvaluateSearchModel();
            searchResult.CustomerId = customerValue;
            searchResult.ProductId = productValue;
            searchResult.PaymentTypeId = paymentValue;
            searchResult.PayCurrencyId = currencyValue;

            if (customerValue == 0) { searchResult.CustomerId = null; }
            if (productValue == 0) { searchResult.ProductId = null; }
            if (paymentValue == 0) { searchResult.PaymentTypeId = null; }
            if (currencyValue == 0) { searchResult.PayCurrencyId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.ddlCustomer.SelectedValueEx = 0;
            this.ddlProduct.SelectedValueEx = 0;
            this.cmbCurrency.SelectedIndex = 0;
            this.cmbPayment.SelectedIndex = 0;
        }

        public PriceEvaluateSearchControl() {
            this.InitializeComponent();            
        }

        private void BindCustomer() {
            CustomerSearchModel searchModel = new CustomerSearchModel() { IsEnabled = true };
            CustomerProcessor customerProcessor = new CustomerProcessor(int.MaxValue);
            List<CustomerViewModel> customerList = customerProcessor.FetchCustomerViewModelList(searchModel).ToList();

            this.ddlCustomer.DisplayMemberEx = "Code";
            this.ddlCustomer.ValueMemberEx = "Key";
            this.ddlCustomer.HeightEx = 300;
            this.ddlCustomer.WidthEx = 400;
            this.ddlCustomer.PageSizeEx = 20;
            this.ddlCustomer.DataBind(new CustomerViewModel(), x => new { x.Code, x.Name, x.Address }, customerList);
        }

        private void BindProduct() {
            ProductSearchModel searchModel = new ProductSearchModel() { IsEnabled = true };
            ProductProcessor productProcessor = new ProductProcessor(int.MaxValue);
            List<ProductViewModel> productList = productProcessor.FetchProductViewModelList(searchModel).ToList();

            this.ddlProduct.DisplayMemberEx = "Code";
            this.ddlProduct.ValueMemberEx = "Key";
            this.ddlProduct.HeightEx = 300;
            this.ddlProduct.WidthEx = 400;
            this.ddlProduct.PageSizeEx = 20;
            this.ddlProduct.DataBind(new ProductViewModel(), x => new { x.Code, x.Name, x.ProductType }, productList);
        }

        private void PriceEvaluateSearchControl_Load(object sender, EventArgs e) {
            this.BindCustomer();
            this.BindProduct();

            this.BindBasics(this.cmbCurrency, BasicsType.Currency);
            this.BindBasics(this.cmbPayment, BasicsType.PaymentType);
        }
    }
}
